const babelParser = require('@babel/parser')
const traverse = require('@babel/traverse')
const { transformFromAstSync } = require('@babel/core')
const env = require('@babel/preset-env')
const transFromCodeByPreset = (preCode) => {
    const ast = babelParser.parse(preCode, {
        sourceType: 'module',
    })
    const { code } = transformFromAstSync(ast, preCode, {
        presets: [env],
    })
    return {
        code,
        ast
    }
}
const getDepByAst = (ast) => {
    const dependencies = []
    traverse.default(ast, {
        ImportDeclaration: ({ node }) => {
            // 基于 import 来获取当前文件需要的依赖
            dependencies.push(node.source.value)
        },
    })
    return dependencies
}

module.exports = {
    getDepByAst,
    transFromCodeByPreset
}